package o2o.web.actions.order.image;

import o2o.config.dao.UserDaoSql;
import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.order.OrderInfoImageMemory;
import o2o.cache.order.OrderInfoMemory;
import o2o.cache.tip.TipImageConfigMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.order.OrderInfo;
import o2o.dao.PO.tip.TipImageConfig;
import o2o.dao.PO.user.UserInfo;
import o2o.dao.VO.common.FileUploadInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;

//店铺实景
@Controller
public class OrderImageAction {

    //新增
    @RequestMapping(method = RequestMethod.POST, value = "/order/image/add")
    @ResponseBody
    public String order_image_add(String token, String orderNo, Long tipId, FileUploadInfo fileUploadInfo) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        OrderInfo orderInfo = OrderInfoMemory.getInstance().getOrderInfo(orderNo);
        if (null == orderInfo) {
            return AjaxResult.errorResult("该订单不存在，请重试");
        }
        if (ValidateUtil.isNull(orderInfo.getPostManUsername())) {
            return AjaxResult.errorResult("您无法操作该订单");
        }
        if (!user.getUsername().equalsIgnoreCase(orderInfo.getPostManUsername())) {
            return AjaxResult.errorResult("该订单不是您的，您无法操作");
        }

        if (ValidateUtil.isNotID(tipId)) {
            return AjaxResult.errorResult("请先选择要上传的图片类型");
        }

        TipImageConfig tipImageConfig = TipImageConfigMemory.getInstance().getById(tipId);
        if (null == tipImageConfig) {
            return AjaxResult.errorResult("您选择的分类已不存在，请刷新后重试");
        }

        if (null != fileUploadInfo) {
            if (!OrderImageMan.saveImage(user.getUsername(), orderNo, tipImageConfig, fileUploadInfo.getImage1())) {
                return AjaxResult.errorResult("图片1保存失败,请重试");
            }
            if (!OrderImageMan.saveImage(user.getUsername(), orderNo, tipImageConfig, fileUploadInfo.getImage2())) {
                return AjaxResult.errorResult("图片2保存失败,请重试");
            }
            if (!OrderImageMan.saveImage(user.getUsername(), orderNo, tipImageConfig, fileUploadInfo.getImage3())) {
                return AjaxResult.errorResult("图片3保存失败,请重试");
            }
            if (!OrderImageMan.saveImage(user.getUsername(), orderNo, tipImageConfig, fileUploadInfo.getImage4())) {
                return AjaxResult.errorResult("图片4保存失败,请重试");
            }
            if (!OrderImageMan.saveImage(user.getUsername(), orderNo, tipImageConfig, fileUploadInfo.getImage5())) {
                return AjaxResult.errorResult("图片5保存失败,请重试");
            }
            if (!OrderImageMan.saveImage(user.getUsername(), orderNo, tipImageConfig, fileUploadInfo.getImage6())) {
                return AjaxResult.errorResult("图片6保存失败,请重试");
            }
            if (!OrderImageMan.saveImage(user.getUsername(), orderNo, tipImageConfig, fileUploadInfo.getImage7())) {
                return AjaxResult.errorResult("图片7保存失败,请重试");
            }
            if (!OrderImageMan.saveImage(user.getUsername(), orderNo, tipImageConfig, fileUploadInfo.getImage8())) {
                return AjaxResult.errorResult("图片8保存失败,请重试");
            }
            if (!OrderImageMan.saveImage(user.getUsername(), orderNo, tipImageConfig, fileUploadInfo.getImage9())) {
                return AjaxResult.errorResult("图片9保存失败,请重试");
            }
            //放入缓存
            OrderInfoImageMemory.getInstance().addOrModifyByOrderNo(orderNo);
        }


        return AjaxResult.successResult();
    }


    //删除
    @RequestMapping(method = RequestMethod.POST, value = "/order/image/delete")
    @ResponseBody
    public String shop_image_delete(String token, String orderNo, Long tipId, String imageIds) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        OrderInfo orderInfo = OrderInfoMemory.getInstance().getOrderInfo(orderNo);
        if (null == orderInfo) {
            return AjaxResult.errorResult("该订单不存在，请重试");
        }
        if (ValidateUtil.isNull(orderInfo.getPostManUsername())) {
            return AjaxResult.errorResult("您无法操作该订单");
        }
        if (!user.getUsername().equalsIgnoreCase(orderInfo.getPostManUsername())) {
            return AjaxResult.errorResult("该订单不是您的，您无法操作");
        }

        if (ValidateUtil.isNotID(tipId)) {
            return AjaxResult.errorResult("请先选择要上传的图片类型");
        }

        TipImageConfig tipImageConfig = TipImageConfigMemory.getInstance().getById(tipId);
        if (null == tipImageConfig) {
            return AjaxResult.errorResult("您选择的分类已不存在，请刷新后重试");
        }

        if (ValidateUtil.isNull(imageIds)) {
            return AjaxResult.errorResult("请选择要删除的图片");
        }

        StringBuilder sb = new StringBuilder();
        sb.append("update ").append(PojoTables.ORDER_INFO_IMAGE).append(" set delFlag = 1,updateUser='").append(user.getUsername())
                .append("' where delFlag = 0 and orderNo = '").append(orderNo).append("' and tipImageId = ").append(tipId).append(" and id in (").append(imageIds).append(") ");
        try {
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("删除失败，请稍后再试 ");
        }

        //在缓存中删除
        OrderInfoImageMemory.getInstance().addOrModifyByOrderNo(orderNo);

        return AjaxResult.successResult();
    }


}
